package com.gxa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.R;
import com.gxa.entity.DayDo;
import com.gxa.entity.DistrictSysContact;
import com.gxa.mapper.DailyAgentMapper;
import com.gxa.service.DailyAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DailyAgentServiceImpl implements DailyAgentService {

    @Autowired
    DailyAgentMapper dailyAgentMapper;

    @Override
    public R FindDailyAgent(Integer currentPage, Integer pageSize, String operator) {
        R r = new R();

        String organization = dailyAgentMapper.findOrganizationByUserName(operator);

        List<DistrictSysContact> districtSysContactList =dailyAgentMapper.findContactByNowAdress(organization);

        Date date = new Date();

        DayDo dayDo = new DayDo();

        for (int i =0;i<districtSysContactList.size();i++){

            DistrictSysContact districtSysContact = districtSysContactList.get(i);

            int dayTemperature =dailyAgentMapper.findTemperatureByIdAndDate(districtSysContact.getId(),date);

            String dayTestResult = dailyAgentMapper.findTestByIdAndDate(districtSysContact.getId(),date);

            Date removeDate = districtSysContact.getRemoveDate();

            if ( String.valueOf(dayTemperature) == null || String.valueOf(dayTemperature)==""){
                    dayDo.setId(districtSysContact.getId());
                    dayDo.setItemName("体温待监测");
                    dayDo.setPatientName(districtSysContact.getName());
                dailyAgentMapper.addDailyAgent(dayDo);
            }

            if ( dayTestResult== null || dayTestResult ==""){
                dayDo.setId(districtSysContact.getId());
                dayDo.setItemName("核酸待监测");
                dayDo.setPatientName(districtSysContact.getName());
                dailyAgentMapper.addDailyAgent(dayDo);
            }

            if ( removeDate == date){
                dayDo.setId(districtSysContact.getId());
                dayDo.setItemName("监测待解除");
                dayDo.setPatientName(districtSysContact.getName());
                dailyAgentMapper.addDailyAgent(dayDo);
            }



        }

        Page page = PageHelper.startPage(currentPage,pageSize);

        List<DayDo> dayDoList = dailyAgentMapper.findDailyAgent();

        int total = (int) page.getTotal();

        Map map = new HashMap();

        map.put("total",total);
        map.put("dayDoList",dayDoList);

        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(map);

        return r;
    }



    @Override
    public R truncateDailyAgent() {
        dailyAgentMapper.truncateDailyAgent();
        System.out.println("每日待办表清除成功");
        return null;
    }
}
